﻿using System;
using System.Configuration;
using System.Xml;


namespace DBScript
{
    /// <summary>
    /// 组件配置文件读取
    /// </summary>
    public class DBScriptSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            //Console.WriteLine(section.InnerXml);
            DBScriptConfig config = new DBScriptConfig();
            foreach (XmlNode child in section.ChildNodes)
            {
                if (child.Name.ToLower() == "setting")
                {
                    if (child.Attributes["IsDebug"] != null)
                    {
                        config.IsDebug = bool.Parse(child.Attributes["IsDebug"].Value);
                    }
                    if (child.Attributes["DebugFilePath"] != null)
                    {
                        config.DebugFilePath = child.Attributes["DebugFilePath"].Value;
                    }
                }
                else if (child.Name.ToLower() == "add")
                {
                    DBConfigNode node = new DBConfigNode();
                    if (child.Attributes["name"] != null)
                    {
                        node.Name = child.Attributes["name"].Value;
                    }
                    if (child.Attributes["connectionString"] != null)
                    {
                        node.ConnectionString = child.Attributes["connectionString"].Value;
                    }
                    if (child.Attributes["dbType"] != null)
                    {

                        node.DbType = (DataBaseType)Enum.Parse(typeof(DataBaseType), child.Attributes["dbType"].Value);
                    }
                    config.DbConfigs.Add(node.Name, node);
                }
            }
            return config;
        }
    }
}
